/*
 * @Descripttion:
 * @Author: voanit
 * @Date: 2022-03-28 08:23:16
 * @LastEditors: voanit
 * @LastEditTime: 2022-04-03 16:19:42
 */
import { loginApi, logout, getInfo, getUserDetailById } from "@/api/user";
import { getToken, setToken, removeToken } from "@/utils/auth";
import { resetRouter } from "@/router";

const getDefaultState = () => {
  return {
    token: getToken(),
    name: "",
    avatar: "",
    userInfo: {},
  };
};

const state = getDefaultState();
// const state = {
//   token: getToken(),
//   name: "",
//   avatar: "",
// };
// const state = {
//   token:''
// };

const mutations = {
  RESET_STATE: (state) => {
    // 浅拷贝
    /* 
    深拷贝：JSON.parse()、递归、
    浅拷贝：赋值， Object.assign ，{...obj}[扩展运算符]=====
    Object.assign(目标对象，值)
    */
    Object.assign(state, getDefaultState());
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo;
  },
};

const actions = {
  // user login
  // login(context, userInfo) {
  login({ commit }, userInfo) {
    // const { username, password } = userInfo;
    return new Promise((resolve, reject) => {
      loginApi(userInfo)
        .then((response) => {
          // console.log(response, 88888);
          // getInfo();校验
          const { data } = response;
          //将token存储到vuex的state中
          commit("SET_TOKEN", response.data);
          //  token持久化====cookie来存储的
          setToken(response.data);
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise(async (resolve, reject) => {
      //  获取用户信息
      // getInfo().then((response) => {
      //   console.log(response, 78);
      //   /* 将用户信息存储到vuex中 */
      //   commit("SET_USERINFO", response.data);
      //   commit("SET_NAME", response.data.username);
      // });
      const response = await getInfo();
      // 获取用户图像
      const response2 = await getUserDetailById(response.data.userId);
      /* 将用户信息存储到vuex中 */
      commit("SET_USERINFO", { ...response.data, ...response2.data });
      commit("SET_NAME", response.data.username);
      commit("SET_AVATAR", response2.data.staffPhoto);
      // getInfo(state.token)
      //   .then((response) => {
      //     const { data } = response;
      //     if (!data) {
      //       return reject("Verification failed, please Login again.");
      //     }
      //     const { name, avatar } = data;
      //     commit("SET_NAME", name);
      //     commit("SET_AVATAR", avatar);
      //     resolve(data);
      //   })
      //   .catch((error) => {
      //     reject(error);
      //   });
    });
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // logout(state.token)
      //   .then(() => {
      //     removeToken(); // must remove  token  first
      //     resetRouter();
      //     commit("RESET_STATE");
      //     resolve();
      //   })
      //   .catch((error) => {
      //     reject(error);
      //   });
      removeToken(); // must remove  token  first
      resetRouter();
      commit("RESET_STATE");
      resolve();
    });
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      removeToken(); // must remove  token  first
      commit("RESET_STATE");
      resolve();
    });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
